﻿Shader "Custom/TestShader18"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
        _BlurSize("BlurSize",float) = 1
    }

    SubShader
    {
        CGINCLUDE
        #include "UnityCG.cginc"

        sampler2D _MainTex;
        float4 _MainTex_ST;
        float4 _MainTex_TexelSize;
        float _BlurSize;

        struct v2f
        {
            float4 pos : SV_POSITION;
            float2 uv[5] : TEXCOORD0;
        };

        v2f vertVertical(appdata_img v)
        {
            v2f o;
            o.pos = UnityObjectToClipPos(v.vertex);
            float2 uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;

            o.uv[0] = uv + _MainTex_TexelSize.xy * float2(0, 2) * _BlurSize;
            o.uv[1] = uv + _MainTex_TexelSize.xy * float2(0, 1) * _BlurSize;
            o.uv[2] = uv + _MainTex_TexelSize.xy * float2(0, 0) * _BlurSize;
            o.uv[3] = uv + _MainTex_TexelSize.xy * float2(0, -1) * _BlurSize;
            o.uv[4] = uv + _MainTex_TexelSize.xy * float2(0, -2) * _BlurSize;

            return o;
        }

        v2f vertHorizonal(appdata_img v)
        {
            v2f o;
            o.pos = UnityObjectToClipPos(v.vertex);
            float2 uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;

            o.uv[0] = uv + _MainTex_TexelSize.xy * float2(2, 0) * _BlurSize;
            o.uv[1] = uv + _MainTex_TexelSize.xy * float2(1, 0) * _BlurSize;
            o.uv[2] = uv + _MainTex_TexelSize.xy * float2(0, 0) * _BlurSize;
            o.uv[3] = uv + _MainTex_TexelSize.xy * float2(-1, 0) * _BlurSize;
            o.uv[4] = uv + _MainTex_TexelSize.xy * float2(-2, 0) * _BlurSize;

            return o;
        }

        fixed4 frag(v2f i) : SV_Target
        {
            float weight[3] = {0.4026, 0.2442, 0.0545};
            fixed3 sum = tex2D(_MainTex, i.uv[2]).rgb * weight[0];
            for (int it = 1; it < 3; it++)
            {
                sum += tex2D(_MainTex, i.uv[2 - it]).rgb * weight[it];
                sum += tex2D(_MainTex, i.uv[2 + it]).rgb * weight[it];
            }

            return fixed4(sum, 1);
        }

        ENDCG
        ZWrite Off
        Cull Off
        ZTest Always
        Pass
        {
            NAME "GAUSSIAN_BLUR_VERTICAL"
            
            CGPROGRAM
            #pragma vertex vertVertical
            #pragma fragment frag
            ENDCG
        }

        Pass
        {
            NAME "GAUSSIAN_BLUR_HORIZONTAL"

            CGPROGRAM
            #pragma vertex vertHorizonal
            #pragma fragment frag
            ENDCG
        }
	}
}
